<template>
  <!-- 注册页面的整体盒子 -->
  <div class="reg-container">
    <!-- 注册的盒子 -->
    <div class="reg-box">
      <!-- 标题的盒子 -->
      <div class="title-box">后台管理系统</div>

      <!-- 注册的表单区域 -->
      <el-form ref="form" :model="form" :rules="rulesObj">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input v-model="form.repassword" type="password" placeholder="请再次确认密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="registerFn" class="btn-reg">注册</el-button>
          <el-link type="info" @click="$router.push('/login')">去登录</el-link>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<script>
import { registerAPI } from '@/api/index.js'
export default {
  name: 'my-register',
  // -----------------------------------------------------------------------------------------------------------------
  data () {
    // 表单检验函数，因为要保证this.form的使用
    const samePwdFn = (rule, value, callback) => {
      if (value !== this.form.password) {
        callback(new Error('两次输入的密码不一致！'))
      } else {
        callback()
      }
    }
    return {
      // 表单数据对象
      form: {
        // 用户名
        username: '',
        // 密码
        password: '',
        // 确认密码
        repassword: ''
      },
      // 表单校验对象
      rulesObj: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,10}$/, message: '用户名必须是1-10位的大小写字母或数字', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15的非空字符',
            trigger: 'blur'
          }
        ],
        repassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: samePwdFn, trigger: 'blur' }
        ]
      }
    }
  },
  // -----------------------------------------------------------------------------------------------------------------
  methods: {

    // 注册的点击事件
    registerFn () {
      // js兜底校验
      this.$refs.form.validate(async valid => {
        if (valid) {
          // 校验通过
          const { data: res } = await registerAPI(this.form)
          console.log(res)
          // ElementUI在vue原型链上添加了弹窗提示，$message
          // 注册失败，提示用户，return阻止代码继续向下执行
          if (res.code !== 0) return this.$message.error(res.message)
          // 注册成功，提示用户
          this.$message.success(res.message)
          this.$router.push('/login')
        } else {
          // 校验未通过，阻止默认的提交行为，表单下面的红色提示会自动出现
          return false
        }
      })
    }
  }
  // -----------------------------------------------------------------------------------------------------------------
}
</script>

<style lang="less" scoped>
.reg-container {
  background: url('@/assets/月光.jpg') center;
  background-size: cover;
  height: 100%;

  .reg-box {
    width: 400px;
    height: 335px;
    background-color: rgba(255,255,255,0.5);
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 0 30px;
    box-sizing: border-box;

    .title-box {
      height: 60px;
      // background: url('@/assets/boate.jpg') center no-repeat;
      font-size: 30px;
      color: rgb(115, 114, 166);
      line-height: 60px;
      text-align: center;
    }

    .btn-reg {
      width: 100%;
    }
  }
}
.btn-reg {
  width: 100%;
}
/deep/.el-input__inner {
  background-color: rgba(255,255,255,0.5);
  color: #409EFF;
}
/deep/.el-form-item__error {
  color: #409EFF;
}
</style>
